<?php

$config = array();

class Coder {
	public function __construct() {
		if( !$this->init() ) {
			exit("Init enviroment failed");
		}
		$this->run();
	}	
		
	public function init() {
		global $config;
		$userConfigPath = $_SERVER['DOCUMENT_ROOT'].'Conf/config.php';
		if( !file_exists($userConfigPath) ) {
			$this->makeProjectDir();
		}
		require_once($userConfigPath);
		
		$this->loadCoreFile();
		return true;
	}
	
	protected function makeProjectDir() {
		$rootPath = $_SERVER['DOCUMENT_ROOT'];
		mkdir($rootPath."/models");
		mkdir($rootPath."/views");
		mkdir($rootPath."/tpl");
		mkdir($rootPath."/Conf");
		mkdir($rootPath."/controllers");
		mkdir($rootPath.'/cache');
		mkdir($rootPath.'/utils');
		mkdir($rootPath.'/servers');
		
		// 鐢熸垚鐢ㄦ埛config.php鏂囦欢
		$data = '<?php defined("APPLICATION_PATH") or define("APPLICATION_PATH", $_SERVER["DOCUMENT_ROOT"]);'; 
		file_put_contents($rootPath.'/Conf/config.php', $data);
		
		// 鐢熸垚IndexController 鍜�indexAction
		$data = '<?php'.PHP_EOL. 
					'class IndexController extends BaseController {'.PHP_EOL. 
					'	public function indexAction(){'.PHP_EOL.
					'		$this->render("index");'.PHP_EOL.
					'	}'.PHP_EOL.
					'}';
		file_put_contents($rootPath.'/controllers/IndexController.php', $data);
		
		// 鐢熸垚 view: index.php 
		$data = '<?php echo "Thinks for use Coder Framework";';
		file_put_contents($rootPath.'/views/index.php', $data);
		
		$data = '<?php function BootStrap($params) {'.PHP_EOL.
				'}';
		file_put_contents($rootPath.'/BootStrap.php', $data);
	}
	
	public function run() {
		new Dispatcher();
	}
	
	protected function loadCoreFile() {
		require_once("Conf/config.php");
		defined('CODER_PATH') 	|| define('CODER_PATH', dirname(__FILE__));
		defined('MODEL_PATH') 	|| define('MODEL_PATH', APPLICATION_PATH.'/models/');
		defined('CONTROL_PATH') || define('CONTROL_PATH', APPLICATION_PATH.'/controllers/');
		defined('VIEW_PATH') 	|| define('VIEW_PATH', APPLICATION_PATH.'/views/');
		defined('SERVER_PATH')	|| define('SERVER_PATH', APPLICATION_PATH.'/servers/');
		defined('TPL_PATH')		|| define('TPL_PATH', APPLICATION_PATH.'/tpl/');
		defined('CACHE_PATH')	|| define('CACHE_PATH', APPLICATION_PATH.'/cache/');
		defined('ULIB_PATH')	|| define('UTIL_PATH', APPLICATION_PATH.'utils/');
		
		defined('CORE_PATH') 	|| define('CORE_PATH', CODER_PATH.'/Core/');
		defined('UTIL_PATH') 	|| define('UTIL_PATH', CODER_PATH.'/Utils/');
		defined('DRIVER_PATH') 	|| define('DRIVER_PATH', CODER_PATH.'/Drivers/');
		defined('LIB_PATH') 	|| define('LIB_PATH', CODER_PATH.'/Lib/');
		
		require_once CORE_PATH.'Loader.php';
		require_once CORE_PATH.'Common.php';
		require_once CORE_PATH.'Dispatcher.php';
	
		require_once CORE_PATH.'BaseModel.php';
		require_once CORE_PATH.'BaseController.php';
		require_once CORE_PATH.'Template.php';
	}
}

new Coder();





